package com.logic.lhub.service;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import com.logic.lhub.event.HomeHeartBeatEvent;

import org.greenrobot.eventbus.EventBus;

public class HeartBeatService extends Service {

    public static final String TAG = "MyService";

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate() executed");
        new Thread(new HeartBeat()).start();

        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                // 捕获未处理的异常
                Log.e(TAG, "Uncaught exception: " + e.getMessage(), e);

                // 停止服务
                stopSelf();

                // 可以选择重启服务或者记录日志等其他操作
            }
        });
    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        // 当外部应用（如用户）尝试停止服务时调用
        stopSelf();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand() executed");

        return super.onStartCommand(intent, flags, startId);
    }

    class HeartBeat implements Runnable {
        private final long interval = 10 * 1000;

        @Override
        public void run() {
            while (true) {
                try {
//                    JSONObject params = new JSONObject();
//                    params.put("storeShortName", AtmosPreference.getCustomStringPre(FusionPre.STORE_NUM));
//                    params.put("cashierNo", AtmosPreference.getCustomStringPre(FusionPre.MACHINE_NUM));
//
//                    HttpHelper.RestClientPostRaw(params, HttpUrl.HEART_BEAT,
//                            new ISuccess() {
//                                @Override
//                                public void onSuccess(String response) {
//                                    Type type = new TypeToken<HeartBeatEntity>() {
//                                    }.getType();
//                                    Gson gson = new Gson();
//                                    HeartBeatEntity mData = gson.fromJson(response, type);
//                                    if (mData.getHeader().getCode().equals(FusionCode.SUCCESS)) {
//                                        if (StringUtil.isNotEmpty(mData.getBody().getTimeStamp())) {
//                                            Intent intent = new Intent(" com.android.lango.setsystemtime");
//                                            intent.putExtra("time", mData.getBody().getTimeStamp());
//                                            sendBroadcast(intent);
//                                        }
//                                    }
//                                }
//                            }, new IError() {
//                                @Override
//                                public void onError(int code, String msg) {
//
//                                }
//                            }, new IFailure() {
//                                @Override
//                                public void onFailure() {
//
//                                }
//                            });

                    EventBus.getDefault().post(new HomeHeartBeatEvent(new Bundle()));


                    Thread.sleep(interval);
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                    break;
                }
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopSelf();
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}
